﻿
//元件引用分析类
namespace n_FuncAndVarLink
{
using System;
using n_Config;
using n_ET;
using n_FunctionList;
using n_MemberType;
using n_Parse;
using n_ParseNet;
using n_StructList;
using n_UnitList;
using n_VarList;
using n_VarType;
using n_VdataList;

public static class FuncAndVarLink
{
	static string UnitName;
	
	static bool StartFuncOK;
	
	//获取元件信息,生成: 元件列表 函数列表 变量列表
	public static void Record()
	{
		StartFuncOK = false;
		Parse.DealWith( ref UnitName, new Parse.MemberListHandle( 成员列表 ) );
		if( !StartFuncOK ) {
			Config.AddStartFunction( VarType.Root + ".main" );
		}
	}
	
	static void 成员列表( int Index )
	{
		for( int i = 1; i < ParseNet.NodeSet[ Index ].Length; ++i ) {
			int index = int.Parse( ParseNet.NodeSet[ Index ][ i ] );
			
			//判断是否元件定义
			if( ParseNet.NodeSet[ index ][ 0 ] == ParseNet.Node.元件 ) {
				元件( index );
				continue;
			}
			//判断是否连接器定义
			if( ParseNet.NodeSet[ index ][ 0 ] == ParseNet.Node.连接器 ) {
				连接器( index );
				continue;
			}
		}
	}
	
	static void 元件( int Index )
	{
		Parse.Unit( ref UnitName, Index, new Parse.MemberListHandle( 成员列表 ) );
	}
	
	static void 连接器( int Index )
	{
		//提取当前连接器的权限
		bool Super = false;
		int SuperIndex = int.Parse( ParseNet.NodeSet[ Index ][ 1 ] );
		if( SuperIndex != -1 ) {
			Super = true;
		}
		//提取第一个成员信息
		int FirstIndex = int.Parse( ParseNet.NodeSet[ Index ][ 2 ] );
		int ErrorIndex1 = 0;
		string FirstName = null;
		Parse.GetMemberNames( UnitName, FirstIndex, ref FirstName, ref ErrorIndex1 );
		
		//提取第二个成员信息
		int SecondIndex = int.Parse( ParseNet.NodeSet[ Index ][ 4 ] );
		int ErrorIndex2 = 0;
		string SecondName = null;
		Parse.GetMemberNames( UnitName, SecondIndex, ref SecondName, ref ErrorIndex2 );
		
		int MemberIndex;
		
		//判断是否已经处理过
		if( ParseNet.NodeSet[ Index ].Length >= 3 && ParseNet.NodeSet[ Index ][ 3 ] == "-1" ) {
			return;
		}
		//判断成员是否为元件类型
		MemberIndex = UnitList.GetDirectIndex( FirstName );
		if( MemberIndex != -1 ) {
			return;
		}
		//判断成员是否为虚拟数据类型
		MemberIndex = VdataList.GetDirectIndex( FirstName );
		if( MemberIndex != -1 ) {
			return;
		}
		//判断成员是否为结构体类型
		MemberIndex = StructList.GetDirectIndex( FirstName );
		if( MemberIndex != -1 ) {
			return;
		}
		//判断成员是否为函数类型
		MemberIndex = FunctionList.GetDirectIndex( FirstName );
		
		/*
		//2016.12.22 新增引用置换, 当目标link所在的Lunit为link类型时, 先解析Lunit的实际元件
		if( MemberIndex == -1 ) {
			string LastName = FirstName.Remove( 0, FirstName.LastIndexOf( '.' ) + 1 );
			string FName = FirstName.Remove( FirstName.LastIndexOf( '.' ) );
			int i = UnitList.GetDirectIndex( FName );
			if( i != -1 ) {
				FirstName = UnitList.Get( i ).TargetMemberName + "." + LastName;
				MemberIndex = FunctionList.GetDirectIndex( FirstName );
			}
		}
		*/
		
		if( MemberIndex != -1 ) {
			if( FunctionList.Get( MemberIndex ).RealRefType != MemberType.RealRefType.link ) {
				if( Super ) {
					FunctionList.Get( MemberIndex ).RealRefType = MemberType.RealRefType.link;
				}
				else {
					ET.WriteParseError( ErrorIndex1, "连接器的左端必须为引用类型元件:" + FirstName );
					return;
				}
			}
			FunctionList.Get( MemberIndex ).TargetMemberName = SecondName;
			return;
		}
		//判断成员是否为静态变量类型
		MemberIndex = VarList.GetDirectStaticIndex( FirstName );
		
		//2016.12.22 新增引用置换, 当目标link所在的Lunit为link类型时, 先解析Lunit的实际元件
		if( MemberIndex == -1 ) {
			string LastName = FirstName.Remove( 0, FirstName.LastIndexOf( '.' ) + 1 );
			string FName = FirstName.Remove( FirstName.LastIndexOf( '.' ) );
			int i = UnitList.GetDirectIndex( FName );
			if( i != -1 ) {
				FirstName = UnitList.Get( i ).TargetMemberName + "." + LastName;
				MemberIndex = VarList.GetDirectStaticIndex( FirstName );
			}
		}
		if( MemberIndex != -1 ) {
			if( VarList.Get( MemberIndex ).RealRefType != MemberType.RealRefType.link ) {
				if( Super ) {
					VarList.Get( MemberIndex ).RealRefType = MemberType.RealRefType.link;
				}
				else {
					ET.WriteParseError( ErrorIndex1, "连接器的左端必须为引用类型属性:" + FirstName );
					return;
				}
			}
			VarList.Get( MemberIndex ).TargetMemberName = SecondName;
			return;
		}
		//判断成员是否为常量类型
		MemberIndex = VarList.GetStaticIndex( FirstName );
		if( MemberIndex != -1 ) {
			if( VarList.Get( MemberIndex ).isConst ) {
				return;
			}
		}
		//判断是否系统配置
		if( FirstName == VarType.Root + "." + VarType.Start ) {
			int FuncIndex = FunctionList.GetIndex( SecondName );
			if( FuncIndex == -1 ) {
				ET.WriteParseError( 0, "起始函数未定义: " + SecondName );
			}
			else {
				Config.AddStartFunction( SecondName );
				StartFuncOK = true;
			}
			return;
		}
		ET.WriteParseError( ErrorIndex1, "不支持的链接成员类型: " + FirstName );
	}
}
}


